package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.Role;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.role.RoleAddRequest;
import cn.edu.ujs.coor.request.role.RoleConfigRequest;
import cn.edu.ujs.coor.request.role.RoleUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list")
    public JsonResponse list () {
        List<Role> list = roleService.list();
        return JsonResponse.success(list);
    }

    @RequestMapping(value = "/add")
    public JsonResponse create (@RequestBody @Valid RoleAddRequest request) {
        roleService.save(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/update")
    public JsonResponse update (@RequestBody @Valid RoleUpdateRequest request) {
        roleService.update(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody @Valid IdRequest request) {
        roleService.delete(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/config")
    public JsonResponse config (@RequestBody @Valid RoleConfigRequest request) {
        roleService.config(request);
        return JsonResponse.success();
    }
}